﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HolidayDataAccessLayer;

namespace HolidayApplication
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            dataAccessLayer = new HolidayDataAccessLayer.HolidayDataAccess();
            boardsDataGridView.DataSource = dataAccessLayer.Boards;
            toursDataGridView.DataSource = dataAccessLayer.Destinations;
            facilitiesDataGridView.DataSource = dataAccessLayer.Facilities;
            transportsDataGridView.DataSource = dataAccessLayer.TransportTypes;
            
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            
            dataAccessLayer.SaveChanges();
        }

        private void cancelChangesFacilitiesBtn_Click(object sender, EventArgs e)
        {
            dataAccessLayer.DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, dataAccessLayer.Facilities);
            facilitiesDataGridView.DataSource = null;
            facilitiesDataGridView.DataSource = dataAccessLayer.Facilities;
        }

        private void cancelChangesTransportsBtn_Click(object sender, EventArgs e)
        {
            dataAccessLayer.DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, dataAccessLayer.TransportTypes);
            transportsDataGridView.DataSource = null;
            transportsDataGridView.DataSource = dataAccessLayer.TransportTypes;
        }

        private void cancelChangesToursBtn_Click(object sender, EventArgs e)
        {
            dataAccessLayer.DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, dataAccessLayer.Destinations);
            toursDataGridView.DataSource = null;
            toursDataGridView.DataSource = dataAccessLayer.Destinations;
        }

        private void cancelChangesBoardsBtn_Click(object sender, EventArgs e)
        {
            dataAccessLayer.DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, dataAccessLayer.Boards);
            boardsDataGridView.DataSource = null;
            boardsDataGridView.DataSource = dataAccessLayer.Boards;
        }

        private void toursDeleteBtn_Click(object sender, EventArgs e)
        {
            for(int i=0;i<toursDataGridView.SelectedRows.Count;i++)
            {
                dataAccessLayer.DeleteDestination(int.Parse(toursDataGridView.SelectedRows[i].Cells["DestinationId"].Value.ToString()));
            }
        }

        private void deleteTransportsBtn_Click(object sender, EventArgs e)
        { 

        }

        private void toursDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (toursDataGridView.SelectedRows.Count == 0) return;
            transportsCheckBoxList.Items.Clear();
            Destination dest = dataAccessLayer.GetDestination(int.Parse(toursDataGridView.SelectedRows[0].Cells["DestinationId"].Value.ToString())).FirstOrDefault();
            transportsCheckBoxList.Items.Clear();
            foreach (var transport in dataAccessLayer.TransportTypes)
            {
                bool isChecked = false;
                var res = (from c in dest.DestinationTransports
                           where c.TransportTypeId == transport.TransportTypeId
                           select c).Count();
                if (res > 0)
                    isChecked = true;
                transportsCheckBoxList.Items.Add(transport.TransportTypeName,isChecked);
                
            }

            facilitiesCheckBoxList.Items.Clear();
            foreach (var facility in dataAccessLayer.Facilities)
            {
                bool isChecked = false;
                var res = (from c in dest.DestinationFacilities
                           where c.FacilityId == facility.FacilityId
                           select c).Count();
                if (res > 0)
                    isChecked = true;
                facilitiesCheckBoxList.Items.Add(facility.FacilityName, isChecked);

            }

            boardCheckBoxList.Items.Clear();
            foreach (var board in dataAccessLayer.Boards)
            {
                bool isChecked = (dest.BoardId == board.BoardId);
                boardCheckBoxList.Items.Add(board.BoardName,isChecked);
            }
        }

        private void transportsCheckBoxList_SelectedValueChanged(object sender, EventArgs e)
        {
            //dataAccessLayer.
        }

  
         
 
    }
}
